/*
 *所有关于dynamic_option类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*dynamic_optionService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.MyTable.IRepositories;
using VOL.Core.Float;
using VOL.Core.ManageUser;
using VOL.Core.DBManager;

namespace VOL.MyTable.Services
{
    public partial class dynamic_optionService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Idynamic_optionRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public dynamic_optionService(
            Idynamic_optionRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            //dynamic_option mainEntity = saveDataModel.MainData.DicToEntity<dynamic_option>();
            WebResponseContent webResponse = new WebResponseContent();
            AddOnExecuted = (dynamic_option currList, object list)=>
            {
                FloatManager.AddProcese(currList);//启动一个新审批流程
                return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }
        public override PageGridData<dynamic_option> GetPageData(PageDataOptions options)
        {
            QueryRelativeExpression = (IQueryable<dynamic_option> queryable) =>
            {
                //1、自己创建或者超级管理都能全程看到
                //2、有没有处理过的task任务

                var user = UserContext.Current.GetUserInfo(UserContext.Current.UserId);

                var doctask = DBServerProvider.DbContext.Set<fk_reimtask>();
                queryable = queryable.Where(x => //user.Role_Id == 1 || user.Role_Id == 2 || 
                                              (
                                            (doctask.Any(t => t.to_username == user.UserName && t.stepid == x.stepid && t.ishandled != 1 && t.reimid == x.dynamic_option_id)//在task中任务存在(判断唯一提交任务的条件)
                                            )));

                return queryable;
            };
            return base.GetPageData(options);
        }
    }
}
